package cst.a722wr.zyfypt_722wr.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import cst.a722wr.zyfypt_722wr.Adapter.FocusAdapter;
import cst.a722wr.zyfypt_722wr.Bean.FocusResult;
import cst.a722wr.zyfypt_722wr.Bean.UserBean;
import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.FocusListListener;
import cst.a722wr.zyfypt_722wr.iface.FocusListener;
import cst.a722wr.zyfypt_722wr.model.FocusListModel;
import cst.a722wr.zyfypt_722wr.model.FocusModel;

public class FocusActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private LinearLayoutManager  layoutManager;
    private FocusAdapter adapter;
    private List<FocusResult<UserBean>> list = null;
    private SharedPreferences sp;
    private String sessionID;
    private int page=1;
    private int lastVisibleItemPosition;
    private int userid;
    private SharedPreferences.Editor editor;

    FocusListener focusListener = new FocusListener() {
        @Override
        public void onResponse(String msg) {

        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(FocusActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    FocusListListener<UserBean> listListener = new FocusListListener<UserBean>() {
        @Override
        public void onResponse(List<FocusResult<UserBean>> beanlist) {
            if(page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(FocusActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_focus);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        sp = getSharedPreferences("ME", MODE_PRIVATE);
        sessionID = sp.getString("session_id",null);
        editor = sp.edit();

        System.out.println("----onCreateView page="+page);

        FocusListModel model=new FocusListModel();
        model.getFocusUserList("user", page, sessionID, listListener);

        initRecyclerView();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    private void initRecyclerView() {
        recyclerView=(RecyclerView)findViewById(R.id.focus_rec);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new FocusAdapter(this);
        adapter.setList(list);
        adapter.setOnItemClickListener(new FocusAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                userid = list.get(position).getBean().getId();
                editor.putInt("userid", userid);
                editor.commit();
                Intent intent = new Intent(FocusActivity.this, CollectActivity.class);
                intent.putExtra("userid", userid);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                userid = list.get(position).getBean().getId();
                AlertDialog.Builder dialog = new AlertDialog.Builder(FocusActivity.this);
                dialog.setTitle("取消关注");
                dialog.setMessage("确定吗？");
                dialog.setCancelable(false);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FocusModel focusModel = new FocusModel();
                        focusModel.unfocus("userfocus", userid, sessionID, focusListener);
                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    FocusListModel model = new FocusListModel();
                    model.getFocusUserList("user", page, sessionID, listListener);
                    System.out.println("----onScrollStateChanged  page="+page);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();
                //lastVisibleItemPosition = list.size() - 1;
            }
        });
    }

}
